/*
 * @Author: xiaoyan
 * @Date: 2021-09-15 16:56:50
 * @LastEditors: xiaoyan
 * @LastEditTime: 2021-09-29 15:58:13
 * @Description: 
 * @FilePath: \vue3-ts-demo\src\store\modules\user.ts
 */
import {
    Store as VuexStore,
    CommitOptions,
    Module,
    DispatchOptions,
    ActionContext,
  } from 'vuex'

  import { RootState } from '@/store'

  import { login, userCurrentInfo } from '@/api/user'
  import { removeToken, setToken,getToken } from '@/utils/cookies'
  import { resetRouter } from '@/router'
  import {RootObject} from "@/model/RootObject"
  import { LoginModel, UserInfoModel } from '@/model/userModel'

export interface UserState {
    token: string
    name: string
    avatar: string
    roles: string[]
}

export type Mutations<S = UserState> = {
    SET_NAME(state:S,name:string):void
    
  
}
type AugmentedActionContext = {
    commit<K extends keyof Mutations>(
        key: K,
        payload: Parameters<Mutations[K]>[1],
      ): ReturnType<Mutations[K]>
} & Omit<ActionContext<UserStore, RootState>, 'commit'>

export interface Actions {
    Login({commit}:AugmentedActionContext,userInfo: { username: string, password: string }):Promise<RootObject<LoginModel>>
    GetUerInfo({commit}:AugmentedActionContext):Promise<RootObject<UserInfoModel>>
    LoginOut({commit}:AugmentedActionContext):void
}

export type UserStore<S = UserState> = Omit<VuexStore<S>, 'getters' | 'commit' | 'dispatch'>
& {
  commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
    key: K,
    payload: P,
    options?: CommitOptions
  ): ReturnType<Mutations[K]>
} & {
  dispatch<K extends keyof Actions>(
    key: K,
    payload: Parameters<Actions[K]>[1],
    options?: DispatchOptions
  ): ReturnType<Actions[K]>
};

export const store: Module<UserState, RootState> = {
    getters:{
        
    },
    state:{
        token: getToken() || '',
        name: '匿名者',
        avatar: '',
        roles: [],
    },
    mutations:{
        SET_TOKEN:(state:UserState,token:string)=>{
            state.token = token;
        },
        SET_NAME:(state:UserState,name:string)=>{
            state.name = name;
        },
        SET_AVATAR:(state:UserState,avatar:string)=>{
            state.avatar = avatar;
        },
        SET_ROLES:(state:UserState,roles:string[])=>{
            state.roles = roles;
        },
    },
    actions:{
        Login({commit},userInfo:{username:string,password:string}){
            return new Promise((resolve, reject) =>{
                login(userInfo).then(res=>{
                    if(res?.resultCode === 0){
                        setToken(res.data.token)
                        commit('SET_TOKEN', res.data.token)

                    }
                    resolve(res);
                }).catch(err=>{
                    reject(err);
                });
            });
        },
        GetUerInfo({commit}){
            userCurrentInfo().then(res=>{
                if(res?.resultCode === 0){
                    commit('SET_NAME', res.data.userInfo[0].cnName);
                    commit('SET_AVATAR', res.data.userInfo[0].staffPhoto);
                    commit('SET_ROLES', res.data.roleId);
                }
            });
        },
        LoginOut({commit}){
            removeToken();
            commit("SET_TOKEN", '')
            commit('SET_NAME', "匿名者");
            commit('SET_AVATAR', "");
            commit("SET_ROLES", [])
            resetRouter()
        },
    },

}